package com.demo.algorithm;

import org.junit.Test;

public class 回文数 {
	public static boolean isPalindrome(int x) {
		String str = String.valueOf(x);
		char[] chars = str.toCharArray();
		for (int i = chars.length - 1; i >= 0; i--) {
			if (chars[i] != chars[chars.length - i - 1]) {
				return false;
			}
		}
		return true;
	}

	public boolean isPalindrome1(int x) {
		String reversedStr = (new StringBuilder(x + "")).reverse().toString();
		return (x + "").equals(reversedStr);
	}

	public boolean isPalindrome2(int x) {
		//边界判断
		if (x < 0) {
			return false;
		}
		int div = 1;
		//
		while (x / div >= 10) {
			div *= 10;
		}
		while (x > 0) {
			int left = x / div;
			int right = x % 10;
			if (left != right) {
				return false;
			}
			x = (x % div) / 10;
			div /= 100;
		}
		return true;
	}

	public boolean method(int x) {
		if (x < 0) {
			return false;
		}
		int div = 1;
		while (x / div > 10) {
			div *= 10;
		}
		while (x > 0) {
			int left = x / div;
			int right = x % 10;
			if (left != right) {
				return false;
			}
			x = x % div / 10;
			div = div / 100;
		}
		return true;
	}

	@Test
	public void test () {
		System.out.println(method(12345432));
	}
}
